<template>
  <el-container style="flex-direction: column; height: 100vh;">
    <!-- 顶部栏 -->
    <HeaderComponent :userInfo="this.userInfo" />

    <!-- 主内容区 -->
    <el-container style="flex-direction: row; height: 100vh;">
      <!-- 侧边栏 -->
      <SidebarComponent />

      <!-- 管理端个人信息展示区域 -->
      <el-main>
        <el-card class="personal-card">
          <div class="user-info">
            <el-avatar :src="userInfo.image" :size="100" class="user-avatar"></el-avatar>
            <div class="user-details">
              <h2 class="username">{{ userInfo.username }}</h2>
              <p class="user-email">{{ userInfo.email }}</p>
              <p class="user-phone">手机号: {{ userInfo.phone || '未绑定' }}</p>
              <p class="user-register-time">注册时间: {{ userInfo.createTime || '未知' }}</p>
            </div>
          </div>

          <!-- 统计信息卡片 -->
          <div class="stats-container">
            <el-card class="stat-card">
              <i class="el-icon-user-solid stat-icon"></i>
              <p class="stat-value">{{ information.userNum }}</p>
              <p>总用户数</p>
            </el-card>
            <el-card class="stat-card">
              <i class="el-icon-s-order stat-icon"></i>
              <p class="stat-value">50</p>
              <p>今日访问量</p>
            </el-card>
            <el-card class="stat-card">
              <i class="el-icon-s-comment stat-icon"></i>
              <p class="stat-value">{{ complaintAdvice.length }}</p>
              <p>待处理投诉</p>
            </el-card>
          </div>

          <!-- 管理功能按钮 -->
          <el-button type="warning" @click="openComplaintDialog">处理投诉</el-button>
        </el-card>
      </el-main>
    </el-container>

    <!-- 投诉建议弹窗 -->
    <el-dialog title="投诉建议" :visible.sync="isComplaintDialogVisible" width="800px">
      <!-- 投诉建议列表 -->
      <el-table :data="complaintAdvice" style="width: 100%" border>
        <el-table-column prop="status" label="类型">
          <template slot-scope="scope">
            {{ scope.row.status === 1 ? '投诉' : '建议' }}
          </template>
        </el-table-column>
        <el-table-column prop="content" label="内容"></el-table-column>
        <el-table-column prop="username" label="提交人" width="120"></el-table-column>
        <el-table-column prop="createTime" label="提交时间" width="160"></el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="text" v-if="scope.row.isDeal === 0" @click="handleComplaint(scope.row)">处理</el-button>
            <label v-if="scope.row.isDeal === 1">已处理</label>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 投诉建议处理弹窗 -->
    <el-dialog title="处理投诉建议" :visible.sync="replyDialogVisible" width="500px">
      <el-form>
        <el-form-item label="回复内容">
          <el-input v-model="complaintForm.reply" type="textarea" :rows="3" placeholder="请输入回复内容"></el-input>
        </el-form-item>
        <el-button @click="isComplaintDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleReply">提交</el-button>
      </el-form>
    </el-dialog>
  </el-container>
</template>

<script>
import HeaderComponent from '@/components/property_components/PropertyHeader.vue';
import SidebarComponent from '@/components/property_components/PropertySidebar.vue';
import { parseJwt } from '@/utils/parseJWT';
import service from '@/utils/request';

export default {
  components: { HeaderComponent, SidebarComponent },
  name: 'PropertyPersonalView',
  data() {
    return {
      // 用户信息
      userInfo: {},
      information: {
        userNum: 0,
        todayUse: 0,
      },
      complaintAdvice: [],
      // 投诉建议弹窗
      isComplaintDialogVisible: false,
      replyDialogVisible: false,
      // 投诉建议表单
      complaintForm: {
        id: '',
        reply: '',
      },
    };
  },
  mounted() {
    // 获取用户信息
    this.userInfo = parseJwt(localStorage.getItem('token')).payload;
    this.getUserInfo();
    this.getComplaintAdvice();
  },

  methods: {
    //获取用户数据
    getUserInfo() {
      service.get(`/api/user/getUserInfo/${this.userInfo.id}`).then(result => {
        result = result.data
        if (result.code == 1) {
          this.userInfo = result.data;
          console.log(this.userInfo);
        } else {
          this.$message.error(result.msg);
        }
      }).catch(error => {
        this.$message.error('获取用户信息失败');
        console.error(error);
      });
      service.get(`/api/admin/user/getUserNum`).then(result => {
        result = result.data
        if (result.code == 1) {
          this.information.userNum = result.data;
        } else {
          this.$message.error(result.msg);
        }
      }).catch(error => {
        this.$message.error('获取用户数量失败');
      });
    },

    getComplaintAdvice() {
      service.get(`/api/admin/complaint/getComplaintAdvice`).then(result => {
        result = result.data
        if (result.code == 1) {
          this.complaintAdvice = result.data;
        } else {
          this.$message.error(result.msg);
        }
      }).catch(error => {
        this.$message.error('获取投诉建议失败');
        console.error(error);
      });
    },

    // 打开投诉建议弹窗
    openComplaintDialog() {
      this.isComplaintDialogVisible = true;
      this.getComplaintAdvice();
    },

    handleComplaint(complaint) {
      this.complaintForm.id = complaint.id;
      this.complaintForm.reply = '';
      this.replyDialogVisible = true;
    },
    handleReply() {
      service.post(`/api/admin/complaint/reply`, this.complaintForm).then(result => {
        result = result.data
        if (result.code == 1) {
          this.$message.success('回复成功');
          this.replyDialogVisible = false;
          this.getComplaintAdvice();
        } else {
          this.$message.error(result.msg);
        }
      }).catch(error => {
        this.$message.error('回复失败');
        console.error(error);
      });
    }
  },
};
</script>

<style scoped>
/* 返回首页按钮 */
.back-button {
  position: fixed;
  top: 20px;
  left: 20px;
  z-index: 1000;
  font-size: 18px;
  background-color: #409EFF;
  border-color: #409EFF;
  color: #fff;
  border-radius: 50px;
  padding: 15px 30px;
  transition: all 0.3s ease;
}

.back-button:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
  transform: scale(1.05);
  box-shadow: 0 5px 15px rgba(64, 158, 255, 0.3);
}

.personal-card {
  margin: 20px auto;
  max-width: 1200px;
  /* 增大宽度 */
  padding: 40px;
  /* 增大内边距 */
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

/* 用户信息展示区域 */
.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
}

.user-avatar {
  margin-right: 20px;
}

.user-details {
  flex: 1;
}

.username {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.user-email {
  margin: 5px 0 0;
  color: #909399;
}

.user-phone,
.user-register-time {
  margin: 5px 0 0;
  color: #606266;
}

/* 统计信息卡片 */
.stats-container {
  display: flex;
  justify-content: space-between;
  margin-bottom: 30px;
}

.stat-card {
  flex: 1;
  margin: 0 10px;
  text-align: center;
  padding: 20px;
  border-radius: 10px;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.stat-icon {
  font-size: 36px;
  color: #409EFF;
  margin-bottom: 10px;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
  margin: 10px 0;
}

/* 设置功能标签页 */
.settings-tabs {
  margin-bottom: 30px;
}

/* 投诉建议按钮 */
.complaint-button {
  margin-top: 30px;
  width: 100%;
  background-color: #e6a23c;
  border-color: #e6a23c;
  color: #fff;
  border-radius: 50px;
  padding: 12px 0;
  font-size: 16px;
  transition: all 0.3s ease;
}

.complaint-button:hover {
  background-color: #ebb563;
  border-color: #ebb563;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(230, 162, 60, 0.3);
}

/* 管理功能按钮 */
.el-button {
  margin-right: 10px;
  margin-bottom: 10px;
}
</style>